#!/bin/bash
# reboots to windows

if [ "$1" = "-h" ]
then
echo "-----------------------------------Winboot Version 2.1.1---------------------------------------"
echo " "
echo "Usage:"
echo " "
echo "winboot -h (brings this screen)"
echo "winboot -install (initial one-time only installation)"
echo "wiboot -update (updates to latest version from repository)"
echo "sudo winboot -set <your_entry_number>  (changes the windows entry number to <your_entry_number>)"
echo " "
echo "Configuration Troubleshooter:"
echo " "
echo "Winboot may need a little configuration change in /etc/default/grub to work,"
echo "if winboot doesn't reboot you into Windows please try the following steps:"
echo "Step 1: Edit the grub configuration file by running: sudo gedit /etc/default/grub"
echo "Step 2: Change the GRUB_DEFAULT parameter so it says GRUB_DEFAULT=\"saved\" then save&exit"
echo " "
echo "--------------------------------------------------------------------------------------------------"
elif [ "$1" = "-install" ] #installation
then
chmod 755 winboot
sudo cp winboot /bin
read -p "Enter your Windows entry in the Grub Menu (Enter 0 if Windows is first, 1-Windows is second, 2-Windows is third etc) : " entry
       sudo echo $entry > entry.cfg
       echo "Windows entry is set to $entry, run {sudo winboot -set <entry number>} if you ever need to change it!"
sudo cp entry.cfg /etc
cd ..
rm -rf Winboot
echo "  "
echo "Latest Winboot version is succesfully installed!"
winboot -h
exit 1
elif  [ "$1" = "-update" ] #update to latest version
then
git clone https://github.com/semiismaili/Winboot.git
cd Winboot
chmod 755 winboot
sudo cp winboot /bin
#sudo cp entry.cfg /etc
cd ..
rm -rf Winboot
echo "  "
echo "Latest Winboot version is succesfully installed!"
exit 1
elif [ "$1" = "-set" ] #entry set
then
       if [ "$2" -ge 0 ] 
       then
       entry=$2
       sudo echo $entry > /etc/entry.cfg
       echo "Windows entry is set to $entry"
       exit 1
       else
        echo "Second argument should be a number!"
       fi
exit 1
elif [ $# -eq 0 ] 
then #actual work
echo "Enter password to reboot to Windows!"
entry=`sudo cat /etc/entry.cfg`
sudo grub-reboot $entry 
                    #run sudo winboot -set <your windows entry number> 
counter=3
echo -n "Rebooting to Windows in "
while [ "$counter" -gt 1 ]
do
echo  -n "$counter..."
counter=$(($counter - 1))
sleep 1
done
echo "1"
reboot
else
echo "Invalid argument, please use winboot -h for help!"
fi
